package com.wisely.ch8_6_1.web;

import com.wisely.ch8_6_1.domain.Location;
import com.wisely.ch8_6_1.domain.Person;
import com.wisely.ch8_6_1.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.List;

@RestController
public class DataController {
    @Autowired
    private IPersonService personService;

    @RequestMapping("/save")
    public void save(Person person) {
        LinkedHashSet<Location> locations = new LinkedHashSet<>();
        Location loc1 = new Location("上海", "2009");
        Location loc2 = new Location("合肥", "2010");
        Location loc3 = new Location("广州", "2011");
        Location loc4 = new Location("马鞍山", "2012");
        locations.add(loc1);
        locations.add(loc2);
        locations.add(loc3);
        locations.add(loc4);

        person.setLocations(locations);
        personService.save(person);
    }

    @RequestMapping(value = "/find/{name}")
    public Person findByName(@PathVariable String name) {
        return personService.findByName(name);
    }

    @RequestMapping(value = "/query/{age}")
    public List<Person> findQuery(@PathVariable Integer age) {
        return personService.withQueryFindByAge(age);
    }
}
